package com.kaibes.imserver.model.data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class SocketStreamData {
	
	private Socket client;
	
	private InputStream is;
	private OutputStream os;

	private InputStreamReader isReader;
	private BufferedReader bfReader;
	private OutputStreamWriter osWriter;
	private BufferedWriter bfWriter;

	public SocketStreamData(Socket client, String chatSet) {
		this.client = client;
		
		try {
			is = client.getInputStream();
			os = client.getOutputStream();

			isReader = new InputStreamReader(is, chatSet);
			osWriter = new OutputStreamWriter(os, chatSet);

			bfReader = new BufferedReader(isReader);
			bfWriter = new BufferedWriter(osWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void changeChatset(String chatSet){
		try {
			isReader = new InputStreamReader(is, chatSet);
			osWriter = new OutputStreamWriter(os, chatSet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void closeStream(){
		try {
			bfReader.close();
			bfWriter.close();
			
			isReader.close();
			osWriter.close();
			
			is.close();
			os.close();
			
			if (!client.isClosed()) {
				client.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public InputStream getIs() {
		return is;
	}

	public void setIs(InputStream is) {
		this.is = is;
	}

	public OutputStream getOs() {
		return os;
	}

	public void setOs(OutputStream os) {
		this.os = os;
	}

	public InputStreamReader getIsReader() {
		return isReader;
	}

	public void setIsReader(InputStreamReader isReader) {
		this.isReader = isReader;
	}

	public BufferedReader getBfReader() {
		return bfReader;
	}

	public void setBfReader(BufferedReader bfReader) {
		this.bfReader = bfReader;
	}

	public OutputStreamWriter getOsWriter() {
		return osWriter;
	}

	public void setOsWriter(OutputStreamWriter osWriter) {
		this.osWriter = osWriter;
	}

	public BufferedWriter getBfWriter() {
		return bfWriter;
	}

	public void setBfWriter(BufferedWriter bfWriter) {
		this.bfWriter = bfWriter;
	}
	
	public void closeClient(){
		try {
			client.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
